﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ResUma
{
    public partial class StatDeFunctiiFrm : StudioForm
    {
        public StatDeFunctiiFrm(ModuleClass mInfo)
            : base(mInfo)
        {
            InitializeComponent();
            this.comboBox1.SelectedIndex = 0;
            workConnString = "ESRPConnectionString";
        }

        private void StatDeFunctiiFrm_Load(object sender, EventArgs e)
        {
            this.pLocMuncaTableAdapter.Connection = DeschideConexiune(workConnString);
            this.pFunctiiTableAdapter.Connection = this.pLocMuncaTableAdapter.Connection;
            this.pFunctiiTableAdapter.Fill(this.persSalDataSet.Ocupatii);
            this.pLocMuncaTableAdapter.Fill(this.persSalDataSet.Departamente,ModuleInfo.CodUnitate);

            foreach (ESRPDataSet.DepartamenteRow dr in persSalDataSet.Departamente)
            {
                this.sectiiCLB.Items.Add(new _itemInfo(dr.Nume,dr.IDDepartament), true);
            }
            foreach (ESRPDataSet.OcupatiiRow dr in persSalDataSet.Ocupatii)
            {
                this.functiiCLB.Items.Add(new _itemInfo(dr.Denumire,dr.IDOcupatie), true);
            }
            AddButton("Genereaza", global::ERPStudio.Properties.Resources._001_06, Genereaza, ToolStripItemAlignment.Right, true);
            AddButton("Posturi Vacante", global::ERPStudio.Properties.Resources.add, Posturi_Vacante, ToolStripItemAlignment.Right,false);
            sectiiCB.Checked = true;
            functiiCB.Checked = true;
        }

        public void Posturi_Vacante(object sender, EventArgs e)
        {
            LocuriVacanteFrm frm = new LocuriVacanteFrm(ModuleInfo);
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        public void Genereaza(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            PreviewRaportFrm frm;

            /*if (this.comboBox1.SelectedIndex == 0)
                frm = new PreviewRaportFrm("StatDeFunctii.rdlc",null,ModuleInfo);
            else
            if (this.comboBox1.SelectedIndex == 1)
                frm = new PreviewRaportFrm("StatDeFunctii2.rdlc",null,ModuleInfo);
            else
            if (this.comboBox1.SelectedIndex == 2)
                frm = new PreviewRaportFrm("StatDeFunctii3.rdlc",null,ModuleInfo);
            else
            if (this.comboBox1.SelectedIndex == 3)
                frm = new PreviewRaportFrm("StatDeFunctii4.rdlc", null,ModuleInfo);
            else*/
                frm = new PreviewRaportFrm("StatDeFunctiiDSP.rdlc",null,ModuleInfo);

            ESRPDataSet persSalDataSet = new ESRPDataSet();

            BindingSource bs;
            bs = new BindingSource(persSalDataSet, "resuma_StatDeFunctii0");
            /*if (this.comboBox1.SelectedIndex == 2)
                bs = new BindingSource(persSalDataSet, "dsc_StatDeFunctiiDSP");*/
            //BindingSource bs1 = new BindingSource(persSalDataSet, "pCfgLuna");

            StringBuilder filtSec=new StringBuilder(), filtFunc=new StringBuilder();
            for (int i = 0; i < sectiiCLB.Items.Count;i++ )
            {

                if (sectiiCLB.GetItemChecked(i))
                {
                    if (filtSec.Length > 0)
                        filtSec.Append(",");
                    filtSec.Append(((_itemInfo)sectiiCLB.Items[i]).Cod.ToString());
                }
            }
            for (int j = 0; j < functiiCLB.Items.Count;j++ )
            {

                if (functiiCLB.GetItemChecked(j))
                {
                    if (filtFunc.Length > 0)
                        filtFunc.Append(",");
                    filtFunc.Append(((_itemInfo)functiiCLB.Items[j]).Cod.ToString());
                }
            }
            SqlDataAdapter ta = new SqlDataAdapter("Select * from [resuma_StatDeFunctii0]", DeschideConexiune(workConnString));
            if (filtSec.Length > 0 || filtFunc.Length > 0)
                ta.SelectCommand.CommandText += " WHERE ";
            if (filtSec.Length > 0)
                ta.SelectCommand.CommandText += "Cod_LM in (" + filtSec +")" ;
            if (filtFunc.Length > 0)
            {
                if(filtSec.Length > 0)
                    ta.SelectCommand.CommandText += " AND ";
                ta.SelectCommand.CommandText += "Cod_Func in (" + filtFunc  + ")";
            }



            if (cBScutitImpozit.Checked)
            {
                if (filtSec.Length > 0 || filtFunc.Length > 0)
                    ta.SelectCommand.CommandText += " AND ";
                ta.SelectCommand.CommandText += " (ScutImpA <> 0) ";

            }


            if (this.tipPerAngaCB.Checked)
            {
                if (filtSec.Length > 0 || filtFunc.Length > 0)
                    ta.SelectCommand.CommandText += " AND ";
                ta.SelectCommand.CommandText += " (Det = '*') ";
            }


            if (this.dataAngajatCB.Checked)
            {
                if (filtSec.Length > 0 || filtFunc.Length > 0)
                    ta.SelectCommand.CommandText += " AND ";
                ta.SelectCommand.CommandText += " DataUnit >=@DataUnit ";
                ta.SelectCommand.Parameters.Add(new SqlParameter("DataUnit", inainteDTP.Value));
            }

            if (this.inainteData.Checked)
            {
                if (filtSec.Length > 0 || filtFunc.Length > 0)
                    ta.SelectCommand.CommandText += " AND ";
                ta.SelectCommand.CommandText += " DataUnit <= @DataUnitInainte ";
                ta.SelectCommand.Parameters.Add(new SqlParameter("DataUnitInainte", inainteDataDTP.Value));
            }

            if (filtSec.Length > 0)
                ta.SelectCommand.CommandText += " AND ";
            ta.SelectCommand.CommandText += " ((DataPlecarii is Null) OR  (DataPlecarii >= @dataplec) )";
            ta.SelectCommand.Parameters.Add(new SqlParameter("dataplec", laDataDTP.Value));

            ta.SelectCommand.CommandText += " order by cod_lm,cod_func";

            if (this.comboBox1.SelectedIndex == 2)
                ta.SelectCommand.CommandText = "Select * from [dsc_StatDeFunctiiDSP] order by COD_LM,COD_FUNC";
            try
            {
                persSalDataSet.Clear();
                persSalDataSet.EnforceConstraints = false;

                if (this.comboBox1.SelectedIndex == 2)
                    ta.Fill(persSalDataSet.resuma_StatDeFunctii0);
                else
                    ta.Fill(persSalDataSet.resuma_StatDeFunctii0);
                //ta.SelectCommand.CommandText = "SELECT * FROM pCfgLuna";
                //ta.Fill(persSalDataSet.pCfgLuna);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            frm.AddDataSource("StatDeFunctii0", bs);
            //frm.AddDataSource("pCfgLuna", bs1);
            switch(this.comboBox1.SelectedIndex)
            {
                case 2:
                    frm.AddParameter("Titlu", "STAT DE FUNCTII", true);
                    break;
                case 3:
                    if(cBScutitImpozit.Checked)
                        frm.AddParameter("Titlu", "Lista persoanelor scutite de impozit", true);    
                    else
                        frm.AddParameter("Titlu", "Stat de functii", true);
                    frm.AddParameter("SalarFunctie", (!SalFuncCB.Checked).ToString(), true);
                    frm.AddParameter("CNP", (!CNPCB.Checked).ToString(), true);
                    frm.AddParameter("Vechime", (!this.VechimeCB.Checked).ToString(), true);
                    frm.AddParameter("IBAN", (!this.IBANCB.Checked).ToString(), true);
                    frm.AddParameter("PersIntretinere", (!this.PersIntretCB.Checked).ToString(), true); 
                    break;
                default:
                    frm.AddParameter("Titlu", "STAT DE FUNCTII", true);
                    break;
                
            }
            frm.MdiParent = MdiParent;
            frm.Show();
            InchideConexiune(ta.SelectCommand.Connection);
        }

        private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.functiiCB.Checked)
            {
                for (int i = 0; i < functiiCLB.Items.Count; i++)
                {
                    functiiCLB.SetItemChecked(i, true);
                }
            }
            else
            {
                for (int i = 0; i < functiiCLB.Items.Count; i++)
                {
                    functiiCLB.SetItemChecked(i, false);
                }
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (sectiiCB.Checked)
            {
                for (int i = 0; i < sectiiCLB.Items.Count; i++)
                {
                    sectiiCLB.SetItemChecked(i, true);
                }
            }
            else 
            {
                for (int i = 0; i < sectiiCLB.Items.Count; i++)
                {
                    sectiiCLB.SetItemChecked(i, false);
                }
            }
            //int a = (int)this.sectiiCLB.CheckedIndices[0];
        }
    }

    public class _itemInfo:Object
    {
        public String Nume = String.Empty;
        public Int32 Cod = 0;
        public _itemInfo(String _nume,Int32 _cod)
        {
            Nume = _nume;
            Cod = _cod;
        }
        override public String ToString()
        {
            return Nume;
        }
        
    }

}
